package gestao_proj;

import java.awt.BorderLayout;
import java.awt.Graphics;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.*;

/**
 * Tabela usada como opcao alternada do diagrama Contem informacoes do Projecto
 * na Parte Sul do Painel
 * 
 * @author Omar Castro
 * 
 */
@SuppressWarnings("serial")
public class Tabela extends JPanel {
	private JTable table;
	private Projecto proj;
	private JScrollPane pane;
	private JPanel info;

	public Tabela(Projecto proj) {
		super(new BorderLayout());
		this.proj = proj;
		info = new JPanel() {
			public void paintComponent(Graphics g) {
				g.drawString("ECT: " + Janela.grafo.projecto.getECT(), 10, 10);
				g.drawString("LCT: " + Janela.grafo.projecto.getLCT(), (this
						.getWidth() >> 1) - 50, 10);
				g.drawString(
						"Slack: " + Janela.grafo.projecto.calcular_slack(),
						this.getWidth() - 100, 10);
			}
		};
		String[] data = { "ID", "Nome da tarefa", "Duracao",
				"Tarefas anteriores", "Tarefas seguintes" };
		DefaultTableModel model = new DefaultTableModel(data, 0);
		table = new JTable(model) {
			public boolean isCellEditable(int rowIndex, int colIndex) {
				return false;
			}
		};
		actualize();
		pane = new JScrollPane(table);
		this.add(pane, BorderLayout.CENTER);
		this.add(info, BorderLayout.SOUTH);
	}

	/**
	 * actualiza a tabela removendo todas as linha e recriando-as
	 */
	void actualize() {
		DefaultTableModel model = (DefaultTableModel) table.getModel();
		while (table.getRowCount() != 0)
			model.removeRow(0);
		Object[] data = new Object[5];
		int j = 0;
		for (Tarefa i : proj.tarefas) {
			data[0] = proj.tarefas.indexOf(i);
			data[1] = i.getnome();
			data[2] = i.getduracao();
			String pai = new String();
			for (Tarefa k : i.precedencias)
				pai += String.valueOf(proj.tarefas.indexOf(k)) + ", ";
			data[3] = pai;
			String filhos = new String();
			for (Tarefa k : i.filhos)
				filhos += String.valueOf(proj.tarefas.indexOf(k)) + ", ";
			data[4] = filhos;
			j++;
			model.addRow(data);
		}
	}

}
